package com.kelvem.saas.workbanch.fsm.container;


import com.kelvem.saas.workbanch.fsm.entity.Entity;
import com.kelvem.saas.workbanch.fsm.core.EntityMappingEnum;
import com.kelvem.saas.workbanch.fsm.entity.EntityMapppingNode;
import org.apache.commons.lang3.tuple.Pair;

import java.util.HashMap;
import java.util.Map;

/**
 * todo kelvem 这个类的作用是?
 * @author kelvem
 */
public class EntityMapppingContainer {
    
    private static Map<Pair<Class<? extends Entity>, Class<? extends Entity>>, EntityMapppingNode> pairMap = new HashMap<>();
    private static Map<Class<? extends Entity>, EntityMapppingNode> entityMap = new HashMap<>();
    
    public static void addMapping(EntityMapppingNode mapping) {
        Pair<Class<? extends Entity>, Class<? extends Entity>> pair = Pair.of(mapping.getLeftClazz(), mapping.getRightClazz());
        pairMap.put(pair, mapping);
        
        if (mapping.getType().equals(EntityMappingEnum.many2one)
                || mapping.getType().equals(EntityMappingEnum.one2one)) {
            entityMap.put(mapping.getLeftClazz(), mapping);
        }
    }
    
    public static EntityMapppingNode getMapping(Class<? extends Entity> leftClazz, Class<? extends Entity> rightClazz) {
        return pairMap.get(Pair.of(leftClazz, rightClazz));
    }
    
    public static EntityMapppingNode getMapping(Class<? extends Entity> leftClazz) {
        return entityMap.get(leftClazz);
    }
    
    public static Map<Class<? extends Entity>, EntityMapppingNode> getEntityMap() {
        return entityMap;
    }
}
